package com.oe.market.modules.sms.service.impl;

import cn.hutool.http.HttpUtil;
import com.oe.market.modules.sms.service.IAuthApiService;

import java.io.IOException;
import java.util.List;

public class Auth51ymServiceImpl implements IAuthApiService {

    protected String getHostUrl() {
        return "http://api.fxhyd.cn/UserInterface.aspx";
    }

    protected String token;
    protected Integer itemId = 0;
    protected String excludeno = "";
    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }




    public static void main(String[] args) throws IOException {
        Auth51ymServiceImpl auth51ymService = new Auth51ymServiceImpl();
        if (!auth51ymService.login("admsojtg","admsojtg")) {
            System.out.println("login error");
            return;
        }
        System.out.println(auth51ymService.getBalance());
    }
    public boolean isSuccess (String response) {
        int success = response.indexOf("success");
        if (success == 0) {
            return true;
        }
        return false;
    }

    public String getBalance() {
        if (null == token || token.isEmpty()) {
            System.out.println("账号未登录");
        }
        String response = HttpUtil.get("http://api.fxhyd.cn/UserInterface.aspx?action=getaccountinfo&token="+token);
        if (isSuccess(response)) {
            return response.split("\\|")[4];
        }
        return null;
    }

    @Override
    public boolean login(String user, String pswd) {
        String ret = HttpUtil.get("http://api.fxhyd.cn/UserInterface.aspx?action=login&username="+user+"&password="+pswd);
        if (isSuccess(ret)) {
            token = ret.split("\\|")[1];
            return true;
        }
        return false;
    }

    @Override
    public boolean loginout() {
        token = "";
        return true;
    }

    @Override
    public String getPhone() {
        String response = HttpUtil.get("http://api.fxhyd.cn/UserInterface.aspx?action=getmobile&token="+token+"&itemid="+itemId+"&excludeno=" + excludeno);
        return null;
    }

    @Override
    public String getPhone(String phone) {
        String response = HttpUtil.get("http://api.fxhyd.cn/UserInterface.aspx?action=getmobile&token="+token+"&itemid="+itemId+"&mobile=" + phone);
        if (isSuccess(response)) {
            return phone;
        }
        return null;
    }

    @Override
    public List<String> getPhone(Integer number) {
        return null;
    }

    @Override
    public String getMessage(String phone) {
        String response = HttpUtil.get("http://api.fxhyd.cn/UserInterface.aspx?action=getsms&token="+token+"&itemid="+itemId+"&mobile=" + phone);
        if (isSuccess(response)) {
            return response.split("\\|")[1];
        }
        return null;
    }

    @Override
    public void sendMessage(String phone, String msg) {

    }

    @Override
    public void sendMessage(List<String> phone, String msg) {

    }

    @Override
    public void checkSend(String phone) {

    }

    @Override
    public boolean pushBan(String phone) {
        String response = HttpUtil.get("http://api.fxhyd.cn/UserInterface.aspx?action=addignore&token="+token+"&itemid="+itemId+"&mobile=" + phone);
        return isSuccess(response);
    }

    @Override
    public boolean release(String phone) {
        String response = HttpUtil.get("http://api.fxhyd.cn/UserInterface.aspx?action=release&token="+token+"&itemid="+itemId+"&mobile=" + phone);
        return isSuccess(response);
    }
}
